<script setup lang="ts" name="editUser">
import { nextTick } from 'vue'
import useEditUser from '@/components/editData/hooks/editUser'
import type { CurrentUser, EditUserExport } from '@/types/loginType'
const {
  form,
  changeImgUrl,
  currUser,
  formSet,
  uploadImg,
  getUserFormData,
  assignCurrUser,
  logOut
} = useEditUser()
defineExpose<EditUserExport>({ getUserFormData })
const emit = defineEmits(['changed'])
const formChanged = async (e: HTMLInputElement) => {
  // 修改 data 值
  formSet.value.add(e)
  assignCurrUser(e.name as Partial<CurrentUser>)
  await nextTick()
  emit('changed')
}
</script>

<template>
  <form ref="form" @change="formChanged($event.target as HTMLInputElement)">
    <div>
      <p>
        <label for="file">
          <img :src="currUser.img" alt="" />
        </label>
        <span><Bottom class="down" /></span>
        <label for="file">
          <img :src="changeImgUrl" alt="" />
        </label>
        <input
          ref="img"
          class="area"
          type="file"
          name="userImg"
          id="file"
          v-show="false"
          @change="uploadImg($event.target as HTMLInputElement)"
          accept="image/*"
        />
      </p>

      <span>
        <p>
          <label for="username">用户名</label>
          <input
            class="area"
            type="text"
            id="username"
            name="username"
            :value="currUser.name"
            required
          />
        </p>
        <p>
          <label for="gender">性别</label>
          <select class="area" id="gender" name="gender" :value="currUser.gender">
            <option value=""></option>
            <option value="male">男</option>
            <option value="female">女</option>
            <option value="other">其他</option>
          </select>
        </p>
        <p>
          <label for="email">邮箱</label>
          <input
            class="area"
            type="email"
            id="email"
            name="email"
            required
            :value="currUser.email"
          />
        </p>
      </span>
    </div>

    <p class="bio">
      <label for="bio">简介</label>
      <textarea class="area" id="bio" name="bio" :value="currUser.bio"></textarea>
    </p>

    <p class="prefer">
      <label for="prefer">偏好</label>
      <textarea class="area" id="prefer" name="prefer" :value="currUser.prefer"></textarea>
    </p>
    <p class="button">
      <button @click.prevent="logOut">切换登录</button>
      <button @click.prevent="logOut">退出登录</button>
    </p>
  </form>
</template>

<style scoped lang="less">
.area {
  color: var(--color1);
}
.down {
  width: 40px;
  height: 40px;
  color: var(--color2);
}
form {
  padding: 10px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-auto-rows: 166px;
  gap: 2px;
  align-items: center;
  justify-items: center;
  > div {
    border: 1px solid var(--color1);
    grid-column: 1 / 4;
    grid-row: 1 / 3;
    display: grid;
    width: 100%;
    height: 100%;
    grid-template-columns: repeat(2, 1fr);
    > p {
      margin: 0 40px;
      > span {
        display: block;
        text-align: center;
        font-size: 20px;
      }
    }
    label[for='file'] {
      margin: 0 auto;
      position: relative;
      width: 126px;
      height: 126px;
      > img {
        position: absolute;
        top: 50%;
        left: 50%;
        translate: -50% -50%;
        width: 96px;
        height: 96px;
        border-radius: var(--radius);
      }
    }

    > span {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      > p {
        margin: 0 auto;
        width: 80%;
        background-color: var(--color1);
        height: 80px;
        > input,
        > select {
          height: 40px;
          width: 100%;
          border: none;
          padding: 6px;
          box-sizing: border-box;
          font-size: 16px;
          border-radius: var(--radius);
        }
      }
    }
  }
  > p {
    align-content: center;
    border: 1px solid var(--color1);
    padding: 10px;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    > label {
      background-color: var(--color1);
      height: 40px;
    }
    > input {
      width: 100%;
      padding: 6px;
      box-sizing: border-box;
      height: 40px;
    }
  }
  > .bio,
  > .prefer {
    grid-column: 4 / 6;
    > textarea {
      padding: 6px;
      box-sizing: border-box;
      border: none;
      height: 106px;
      width: 100%;
      resize: none;
      border-radius: var(--radius);
    }
  }
  > .button {
    grid-column: 5 / 6;
    display: block;
    width: 100%;
    height: 100%;
    > button {
      cursor: pointer;
      display: block;
      margin: 10px auto;
      width: 60%;
      height: 40px;
      border: none;
      border-radius: var(--radius);
    }
  }
}
label {
  display: block;
  color: #fff;
  height: 40px;
  line-height: 40px;
  text-align: center;
}
</style>
